//
//  NewFeatureView.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/13.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit
let numberOfPage = 4
class NewFeatureView: UIView {

    lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.bounces = false
        return scrollView
    }()
    lazy var pageController : UIPageControl = {
       let pageController = UIPageControl()
        pageController.pageIndicatorTintColor = UIColor.blackColor()
        pageController.currentPageIndicatorTintColor = ThemeColor
        pageController.numberOfPages = numberOfPage
        return pageController
    }()
    override init(frame: CGRect)
    {
        super.init(frame: screenBounds)
        self.backgroundColor = UIColor.clearColor()
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
// MARK: - 配置UI控件
extension NewFeatureView
{
    func setUI()
    {
        addSubview(scrollView)
        addSubview(pageController)
        for i in 1...4
        {
            let imageView = UIImageView(imageName: "new_feature_\(i)")
            imageView.frame = CGRectMake(CGFloat(i-1) * screenWidth, 0, screenWidth, screenHeight)
            scrollView.addSubview(imageView)
        }
        scrollView.contentSize = CGSizeMake(screenWidth * CGFloat(self.pageController.numberOfPages + 1), 0)
        scrollView.frame = screenBounds
        
        pageController.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.center.y).offset(200)
        }
        
    }
    override func layoutSubviews() {
        
    }
}
// MARK: - ScrollView代理
extension NewFeatureView : UIScrollViewDelegate
{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        let currentPage = Int(offsetX/screenWidth + 0.5)
        pageController.currentPage = currentPage
        pageController.hidden = offsetX > (CGFloat( pageController.numberOfPages - 1  ) + 0.3) * screenWidth
    }
}